package com.practicesuite.controllayer;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JButton;

import com.practicesuite.systemlayer.Doctor;
import com.practicesuite.systemlayer.DoctorOrder;
import com.practicesuite.systemlayer.Invoice;
import com.practicesuite.systemlayer.Nurse;
import com.practicesuite.systemlayer.Prescription;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.TreatmentRecord;
import com.practicesuite.systemlayer.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Panel for viewing details for a specific treatment record.
 * 
 * @author James
 *
 */
public class ViewTreatmentPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public MainWindow window;
    private GregorianCalendar date = new GregorianCalendar();
    private Date d;
    private int[] vitalSigns;
    private DoctorOrder doctorOrder;
    private ArrayList<Prescription> prescriptions;
    private Prescription prescription;
    private Object[] labWorkList;
    private Doctor doctor;
    private Nurse nurse;
    
    private String prescriptionString = "N/A";
    private String labWorkString = "N/A";
    private String followUpString = "N/A";
    private String otherInstructionsString = "N/A";

    /**
     * Create the panel.
     */
    public ViewTreatmentPanel(MainWindow container, final TreatmentRecord treatmentRecord, final User user) {
        this.window = container;
        setLayout(new GridLayout(0, 1, 0, 0));
        setPreferredSize(new Dimension(500, 400));
        
        try {
            doctorOrder = SystemDriver.getDoctorOrderFromTreatmentRecordId(treatmentRecord.getTreatRecId());
            System.out.println(treatmentRecord.getTreatRecId());
            prescriptions = new ArrayList<Prescription>(doctorOrder.getpList());
           // prescriptions = doctorOrder.getpList();
            //prescriptions.pop()
            prescription = prescriptions.get(0);
            //(Prescription)(prescriptions[0]);
            prescriptionString = prescription.getInstructions();
            labWorkList = doctorOrder.getLabWorkPreformed().toArray();
            labWorkString = (String)(labWorkList[0]);
            followUpString = doctorOrder.getFollowUpInstructions();
            otherInstructionsString = doctorOrder.getOtherInstructions();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        date.setTime(treatmentRecord.getDateTime());
        d = treatmentRecord.getTime();
        vitalSigns = treatmentRecord.getVitalSigns();
        
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 1, 0, 0));
        panel.setPreferredSize(new Dimension(500, 550));
        
        JLabel lblTreatmentRecord = new JLabel("Treatment Record: " + 
                date.get(GregorianCalendar.MONTH) + "/" + 
                date.get(GregorianCalendar.DAY_OF_MONTH) + "/" + 
                date.get(GregorianCalendar.YEAR));
        panel.add(lblTreatmentRecord);
        lblTreatmentRecord.setFont(new Font("Tahoma", Font.PLAIN, 24));
        lblTreatmentRecord.setHorizontalAlignment(SwingConstants.CENTER);
        
        JPanel panel_1 = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        panel.add(panel_1);
        
        JLabel lblTime = new JLabel(" Time: ");
        panel_1.add(lblTime);
        lblTime.setFont(new Font("Tahoma", Font.BOLD, 12));
        
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
        
        System.out.println(d);
        System.out.println(treatmentRecord.getTime());
        JLabel lblTimeData = new JLabel(sdf.format(d));
        panel_1.add(lblTimeData);
        lblTimeData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        JPanel panel_2 = new JPanel();
        FlowLayout flowLayout_1 = (FlowLayout) panel_2.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEFT);
        panel.add(panel_2);
        
        JLabel lblTreatingDoctor = new JLabel(" Treating doctor: ");
        panel_2.add(lblTreatingDoctor);
        lblTreatingDoctor.setFont(new Font("Tahoma", Font.BOLD, 12));
        
        try {
            doctor = SystemDriver.getDoctorById(treatmentRecord.getTreatingDoctor());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
        }
        JLabel lblTreatingDoctorData = new JLabel(doctor.getName());
        panel_2.add(lblTreatingDoctorData);
        lblTreatingDoctorData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        JScrollPane scrollPane = new JScrollPane(panel);
        
        JPanel panel_3 = new JPanel();
        FlowLayout flowLayout_2 = (FlowLayout) panel_3.getLayout();
        flowLayout_2.setAlignment(FlowLayout.LEFT);
        panel.add(panel_3);
        
        JLabel lblAttendingNurse = new JLabel(" Attending nurse: ");
        panel_3.add(lblAttendingNurse);
        lblAttendingNurse.setFont(new Font("Tahoma", Font.BOLD, 12));
        
        try {
            nurse = SystemDriver.getNurseFromID(treatmentRecord.getAttendingNurse());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
        }
        JLabel lblAttendingNurseData = new JLabel(nurse.getName());
        panel_3.add(lblAttendingNurseData);
        lblAttendingNurseData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        JPanel panel_6 = new JPanel();
        FlowLayout flowLayout_5 = (FlowLayout) panel_6.getLayout();
        flowLayout_5.setAlignment(FlowLayout.LEFT);
        panel.add(panel_6);
        
        JLabel lblCheifComplaint = new JLabel(" Chief complaint: ");
        panel_6.add(lblCheifComplaint);
        lblCheifComplaint.setFont(new Font("Tahoma", Font.BOLD, 12));
        
        JLabel lblCheifComplaintData = new JLabel(treatmentRecord.getChiefComplaint());
        panel_6.add(lblCheifComplaintData);
        lblCheifComplaintData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        JPanel panel_9 = new JPanel();
        panel.add(panel_9);
        FlowLayout flowLayout_8 = (FlowLayout) panel_9.getLayout();
        flowLayout_8.setAlignment(FlowLayout.LEFT);
        
        JLabel lblDiagnosis = new JLabel(" Diagnosis: ");
        panel_9.add(lblDiagnosis);
        lblDiagnosis.setFont(new Font("Tahoma", Font.BOLD, 12));
        
        JLabel lblDiagnosisData = new JLabel(treatmentRecord.getDiagnosis());
        panel_9.add(lblDiagnosisData);
        lblDiagnosisData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        JPanel panel_4 = new JPanel();
        FlowLayout flowLayout_3 = (FlowLayout) panel_4.getLayout();
        flowLayout_3.setAlignment(FlowLayout.LEFT);
        panel.add(panel_4);
        
        JLabel lblDoctorsOrders = new JLabel(" Doctor's Orders: ");
        panel_4.add(lblDoctorsOrders);
        lblDoctorsOrders.setFont(new Font("Tahoma", Font.PLAIN, 16));
        
        JLabel lblPerscription = new JLabel(" Perscription: ");
        panel_4.add(lblPerscription);
        lblPerscription.setFont(new Font("Tahoma", Font.BOLD, 12));
        
        JLabel label = new JLabel(prescriptionString);
        label.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_4.add(label);
        
        JLabel lblLabWork = new JLabel(" Lab work: ");
        panel_4.add(lblLabWork);
        lblLabWork.setHorizontalAlignment(SwingConstants.LEFT);
        lblLabWork.setFont(new Font("Tahoma", Font.BOLD, 12));
        
        JLabel label_1 = new JLabel(labWorkString);
        label_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_4.add(label_1);
        
        JPanel panel_5 = new JPanel();
        FlowLayout flowLayout_4 = (FlowLayout) panel_5.getLayout();
        flowLayout_4.setAlignment(FlowLayout.LEFT);
        panel.add(panel_5);
        
        JLabel lblFollowup = new JLabel("           Follow-up: ");
        panel_5.add(lblFollowup);
        lblFollowup.setHorizontalAlignment(SwingConstants.LEFT);
        lblFollowup.setFont(new Font("Tahoma", Font.BOLD, 12));
        
        JLabel label_2 = new JLabel(followUpString);
        label_2.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_5.add(label_2);
        
        JLabel lblOtherInstructions = new JLabel(" Other instructions: ");
        panel_5.add(lblOtherInstructions);
        lblOtherInstructions.setHorizontalAlignment(SwingConstants.LEFT);
        lblOtherInstructions.setFont(new Font("Tahoma", Font.BOLD, 12));
        
        JLabel label_3 = new JLabel(otherInstructionsString);
        label_3.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_5.add(label_3);
        
        JPanel panel_7 = new JPanel();
        FlowLayout flowLayout_6 = (FlowLayout) panel_7.getLayout();
        flowLayout_6.setAlignment(FlowLayout.LEFT);
        panel.add(panel_7);
        
        JLabel lblVitalSigns = new JLabel(" Vital signs: ");
        panel_7.add(lblVitalSigns);
        lblVitalSigns.setFont(new Font("Tahoma", Font.PLAIN, 16));
        
        JLabel lblBloodPressure = new JLabel(" Blood Pressure:");
        lblBloodPressure.setFont(new Font("Tahoma", Font.BOLD, 12));
        panel_7.add(lblBloodPressure);
        
        JLabel lblBloodPressureData = new JLabel(Integer.toString(vitalSigns[0]));
        lblBloodPressureData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_7.add(lblBloodPressureData);
        
        JLabel lblPulse = new JLabel(" Pulse:");
        lblPulse.setFont(new Font("Tahoma", Font.BOLD, 12));
        panel_7.add(lblPulse);
        
        JLabel lblPulseData = new JLabel(Integer.toString(vitalSigns[1]));
        lblPulseData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_7.add(lblPulseData);
        
        JPanel panel_8 = new JPanel();
        FlowLayout flowLayout_7 = (FlowLayout) panel_8.getLayout();
        flowLayout_7.setAlignment(FlowLayout.LEFT);
        panel.add(panel_8);
        
        JLabel lblTemperature = new JLabel("           Temperature:");
        panel_8.add(lblTemperature);
        lblTemperature.setFont(new Font("Tahoma", Font.BOLD, 12));
        
        JLabel lblTemperatureData = new JLabel(Integer.toString(vitalSigns[2]));
        lblTemperatureData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_8.add(lblTemperatureData);
        
        JLabel lblHeight = new JLabel(" Height:");
        lblHeight.setFont(new Font("Tahoma", Font.BOLD, 12));
        panel_8.add(lblHeight);
        
        JLabel lblHeightData = new JLabel(Integer.toString(vitalSigns[3]));
        lblHeightData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_8.add(lblHeightData);
        
        JLabel lblWeight = new JLabel(" Weight:");
        lblWeight.setFont(new Font("Tahoma", Font.BOLD, 12));
        panel_8.add(lblWeight);
        
        JLabel lblWeightData = new JLabel(Integer.toString(vitalSigns[4]));
        lblWeightData.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_8.add(lblWeightData);
        
        JPanel panel_10 = new JPanel();
        panel.add(panel_10);
        
        JButton btnViewInvoice = new JButton("View Invoice");
        btnViewInvoice.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    List<Invoice> iList = SystemDriver.getInvoice(treatmentRecord.getTreatRecId());
                    Invoice i = iList.get(0);
                    window.gotoPanel("viewInvoice", i, user);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
                    e.printStackTrace();
                }
            }
        });
        btnViewInvoice.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_10.add(btnViewInvoice);
        
        JButton btnReturnToHome = new JButton("Return to Home");
        panel_10.add(btnReturnToHome);
        btnReturnToHome.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int eUID = user.getEUID();
                if (eUID == 0) {
                    window.gotoPanel("adminHome", user);
                }
                else if (eUID == 1) {
                    window.gotoPanel("doctorHome", user);
                }
                else if (eUID == 2) {
                    window.gotoPanel("nurseHome", user);
                }
                else if (eUID == 3) {
                    window.gotoPanel("patientHome", user);
                }
            }
        });
        btnReturnToHome.setFont(new Font("Tahoma", Font.PLAIN, 14));
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        add(scrollPane);

    }

}
